package com.sky.controller.admin;


import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "分类接口操作")
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {
    //注入sevice层接口
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加分类
     */
    @PostMapping
    @ApiOperation("添加分类")
    public Result addCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("添加分类接口参数{}", categoryDTO);
        categoryService.addCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> listPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询参数{}", categoryPageQueryDTO);
        PageResult categoryList = categoryService.listPage(categoryPageQueryDTO);
        return Result.success(categoryList);
    }

    /**
     * 根据id删除分类
     */
    @DeleteMapping
    @ApiOperation("根据id删除分类")
    public Result deleteCategory(Long id) {
        categoryService.deleteCategory(id);
        return Result.success();
    }

    /**
     * 启用和禁用功能
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用和禁用功能")
    public Result onOrOff(@PathVariable Integer status, Long id) {
        log.info("启用和禁用功能参数{}{}", status, id);
        categoryService.onOrOff(status, id);
        return Result.success();
    }

    /**
     * 修改分类
     */
    @PutMapping
    @ApiOperation("修改分类")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改参数{}",categoryDTO);
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     */
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> typeQuery(Integer type) {
        List<Category> category = categoryService.typeQuery(type);
        return Result.success(category);
    }
}
